package uz.edo.test;

import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.Iterator;

import uz.edo.db.bean.User;
import uz.edo.db.bean.Privilege;
import uz.edo.db.bean.Role;
import uz.edo.db.manager.UserManager;
import uz.edo.db.manager.PrivilegeManager;
import uz.edo.db.manager.RoleManager;

/**
 * Created by IntelliJ IDEA.
 * User: said
 * Date: 1/17/12
 * Time: 11:26 AM
 * To change this template use File | Settings | File Templates.
 */
public class test {

    public static void main(String[] args) {
        PrivilegeManager privilegeManager = PrivilegeManager.getInstance();
        RoleManager roleManager = RoleManager.getInstance();
        UserManager userManager = UserManager.getInstance();

        List<User> users = null;
        List<Role> roleList = null;
        List<Privilege> privilegeList = null;

        Privilege readOnly = null;
        Privilege readWrite = null;
        Role role = null;

        try {
            users = userManager.getAll();
            roleList = roleManager.getAll();
            privilegeList = privilegeManager.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(users == null || users.size() == 0) {
            if(privilegeList == null || privilegeList.size() == 0){
                readOnly = new Privilege();
                readOnly.setName("readOnly");
                readWrite = new Privilege();
                readWrite.setName("readWrite");
                try {
                    privilegeManager.saveOrUpdate(readOnly);
                    privilegeManager.saveOrUpdate(readWrite);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if(roleList == null || roleList.size() == 0){
                role = new Role();
                role.setName("su");
                Set privileges = new HashSet();
                privileges.add(readWrite);
                role.setPrivileges(privileges);
                try {
                    roleManager.saveOrUpdate(role);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            User user = new User();
            user.setUserName("sys");
            user.setPassword("sys");
            user.setActive("Y");
            user.setFirstName("bahrom");
            user.setLastName("Sooltonov");
            user.setMiddleName("Saidakromovich");
            Set roles = new HashSet();
            roles.add(role);
            user.setRoles(roles);

            try {
                userManager.saveOrUpdate(user);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            for(User emp : users){
                Set roleSet = emp.getRoles();
                Iterator iterator = roleSet.iterator();
                while (iterator.hasNext()){
                    Role r = (Role)iterator.next();
                    r.getName();
                    Set privSet = r.getPrivileges();
                    Iterator privIterator = privSet.iterator();
                    while (privIterator.hasNext()){
                        Privilege p = (Privilege)privIterator.next();
                        p.getName();
                    }
                    r.getName();
                }

                emp.isPrivileged("readWrite");
                emp.isPrivileged("readOnly");
            }

            try {
                userManager.getUser("sys", "sys");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
